package com.ilas.controller;

import com.ilas.config.DynamicDataSourceContextHolder;
import com.ilas.entity.BizUser;
import com.ilas.entity.TenantInfo;
import com.ilas.service.UserService;
import com.ilas.service.impl.TenantInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 <p>${Description}</p>
 @author Luzck
 @date 2021/4/1 9:42 */
@Slf4j
@RestController
@RequestMapping("tenant")
public class TenantController {
    @Resource
    private TenantInfoServiceImpl tenantService;

    @Resource
    private UserService userService;

    @RequestMapping("all")
    public List<TenantInfo> findAll() {
        DynamicDataSourceContextHolder.clearDataSourceKey();
        DynamicDataSourceContextHolder.setDataSourceKey("master");
        return tenantService.findList();
    }


    @RequestMapping("add")
    public List<TenantInfo> add(TenantInfo tenant) {
        System.out.println("before:" + DynamicDataSourceContextHolder.getDataSourceKey());
        TenantInfo t = tenantService.add(tenant);
        if (t != null) {
            DynamicDataSourceContextHolder.clearDataSourceKey();
            DynamicDataSourceContextHolder.setDataSourceKey(t.getTenantId());
        }
        System.out.println("after:" + DynamicDataSourceContextHolder.getDataSourceKey());

        List<BizUser> list = userService.getList();
        System.out.println(list.isEmpty());

        List<TenantInfo> tenantList = tenantService.findList();
        log.info("{}", tenantList);
        return tenantList;
    }
}
